<?php

namespace App\Http\Controllers\Admin\Setting;

use App\Http\Controllers\Admin\Controller;
use App\Models\WebsiteConfig;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;

class WebSiteController extends Controller
{
    public function index(): Response
    {
        $data = WebsiteConfig::query()->pluck('value', 'key');
        return $this->inertia('setting/website/index', [
            'data' => $data,
        ]);
    }

    public function update(): RedirectResponse
    {
        $params = request()->validate([
            'name' => 'required|string',
            'short_name' => 'required|string',
            'domain' => 'required|url',
            'title' => 'required|string',
            'keywords' => 'required|string',
            'description' => 'required|string',
            'pc_logo' => 'required|string',
            'h5_logo' => 'required|string',
            'copyright' => 'required|string',
            'icp' => 'required|string',
            'status' => 'required|integer',
            'maintenance_reason' => 'nullable|string',
            'contact_phone' => 'nullable|string',
            'contact_email' => 'nullable|string',
            'contact_address' => 'nullable|string',
            'contact_qq_qrcode' => 'nullable|string',
            'contact_wechat_qrcode' => 'nullable|string',
        ]);
        $params['status'] = (int) $params['status'];
        foreach ($params as $key => $value) {
            WebsiteConfig::query()->where('key', $key)->update(['value' => $value]);
            WebsiteConfig::clearCache($key);
        }

        return redirect()->back()->with('message', '保存成功');
    }
}
